package com.wust.supermarket.controller;

import com.wust.supermarket.domain.Member;
import com.wust.supermarket.domain.Response;
import com.wust.supermarket.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/member/list")
    public String listMember(Model model) {
        List<Member> members = memberService.listAllMembers();
        model.addAttribute("members", members);
        return "member/listMember";
    }

    @GetMapping("/member/add")
    public String showAddMember() {
        return "member/addMember";
    }

    @ResponseBody
    @GetMapping("/member/delete/{id}")
    public Response deleteInstock(@PathVariable("id") int memberId) {
        if (memberService.deleteMember(memberId) > 0)
            return new Response<>(1, "删除成功", null);
        else
            return new Response<>(-1, "删除失败", null);
    }

    @ResponseBody
    @PostMapping("/member/update")
    public Response updateInstock(@RequestBody Member member) {
        System.out.println(member);
        if (memberService.updateMember(member) > 0)
            return new Response<>(1, "修改成功", null);
        else
            return new Response<>(-1, "修改失败", null);
    }

    @ResponseBody
    @PostMapping("/member/add")
    public Response insertInstock(@RequestBody Member member) {
        member.setMemberIntegral(0);
        if (memberService.insertMember(member) > 0)
            return new Response<>(1, "插入成功", null);
        else
            return new Response<>(-1, "插入失败", null);
    }
}
